package com.youku.core.view.components
{
   import flash.display.Sprite;
   import com.youku.interfaces.IPlayerProxy;
   import flash.net.NetStream;
   import flash.media.Video;
   import com.youku.data.PlayerConstant;
   import com.youku.PlayerConfig;
   import com.youku.utils.Tweener;
   
   public class VideoDisplay extends Sprite
   {
      
      public function VideoDisplay(param1:IPlayerProxy, param2:BaseCore) {
         super();
         this._playerProxy = param1;
         this._core = param2;
      }
      
      protected var _width:Number;
      
      protected var _height:Number;
      
      protected var _mode:String;
      
      protected var _rotation:Number = 0;
      
      protected var _isRenderSize:Boolean = true;
      
      protected var _playerProxy:IPlayerProxy;
      
      protected var _core:BaseCore;
      
      protected var _isPreShowing:Boolean;
      
      protected var _showingNum:Number = 0;
      
      protected var _curNS:NetStream;
      
      protected var _video:Video;
      
      protected var _bakVideo:Video;
      
      public function init(param1:Number, param2:Number) : void {
         this._isRenderSize = true;
         this._width = param1;
         this._height = param2;
         this._isPreShowing = false;
         this._showingNum = 0;
         this._video = new Video();
         this._video.smoothing = true;
         this._video.visible = true;
         this._bakVideo = new Video();
         this._bakVideo.smoothing = true;
         this._bakVideo.visible = false;
         addChild(this._video);
         addChild(this._bakVideo);
         this.setWH(this._width,this._height);
      }
      
      public function clear() : void {
         if(this._video)
         {
            this._video.clear();
         }
         if(this._bakVideo)
         {
            this._bakVideo.clear();
         }
      }
      
      public function updateVideo(param1:NetStream) : Boolean {
         if(param1 == null)
         {
            return false;
         }
         this._curNS = param1;
         var _loc2_:* = true;
         var _loc3_:* = false;
         if(_loc2_)
         {
            this.chooseOneVideoToShow(param1);
            _loc3_ = false;
         }
         return _loc3_;
      }
      
      public function preShow(param1:NetStream) : void {
         if(this._isPreShowing)
         {
            return;
         }
         this._isPreShowing = true;
         if(this._showingNum == 0)
         {
            this._bakVideo.visible = false;
            this._bakVideo.attachNetStream(param1);
         }
         else if(this._showingNum == 1)
         {
            this._video.visible = false;
            this._video.attachNetStream(param1);
         }
         
      }
      
      public function changeShow() : void {
         this._isPreShowing = false;
         if(this._showingNum == 0)
         {
            this._showingNum = 1;
            this._video.visible = false;
            this._bakVideo.visible = true;
         }
         else if(this._showingNum == 1)
         {
            this._showingNum = 0;
            this._video.visible = true;
            this._bakVideo.visible = false;
         }
         
      }
      
      private function chooseOneVideoToShow(param1:NetStream) : void {
         if(this._showingNum == 0)
         {
            this._video.attachNetStream(param1);
            this._video.visible = true;
            this._bakVideo.visible = false;
         }
         else
         {
            this._video.visible = false;
            this._bakVideo.attachNetStream(param1);
            this._bakVideo.visible = true;
         }
      }
      
      public function setWH(param1:Number, param2:Number, param3:Boolean = true) : void {
         this._width = param1;
         this._height = param2;
         this._isRenderSize = param3;
         this.setScreenMode(this._mode,true);
      }
      
      public function setScreenMode(param1:String, param2:Boolean) : void {
         this._mode = param1;
         var _loc3_:Number = this._width;
         var _loc4_:Number = this._height;
         switch(this._mode)
         {
            case PlayerConstant.SCREEN_MODE_50:
               _loc3_ = int(this._width * 0.5);
               _loc4_ = int(this._height * 0.5);
               break;
            case PlayerConstant.SCREEN_MODE_75:
               _loc3_ = int(this._width * 0.75);
               _loc4_ = int(this._height * 0.75);
               break;
            case PlayerConstant.SCREEN_MODE_100:
               _loc3_ = int(this._width);
               _loc4_ = int(this._height);
               break;
         }
         this._video.rotation = this._rotation;
         this._bakVideo.rotation = this._rotation;
         this.adjustWH(_loc3_,_loc4_,param2);
      }
      
      protected function adjustWH(param1:Number, param2:Number, param3:Boolean) : void {
         var _loc8_:* = false;
         var _loc11_:* = NaN;
         var _loc12_:* = NaN;
         var _loc4_:Number = 0;
         var _loc5_:Number = 0;
         var _loc6_:Number = 0;
         var _loc7_:Number = 0;
         if(this._core.currentVideoGotMetadata == false)
         {
            return;
         }
         var _loc9_:Number = this._core.currentVideoRealWidth;
         var _loc10_:Number = this._core.currentVideoRealHeight;
         if(this._video.rotation % 180 == 0)
         {
            if(_loc9_ / _loc10_ > param1 / param2)
            {
               _loc4_ = param1;
               _loc5_ = param1 * _loc10_ / _loc9_;
            }
            else
            {
               _loc5_ = param2;
               _loc4_ = param2 * _loc9_ / _loc10_;
            }
            _loc8_ = false;
         }
         else
         {
            _loc11_ = _loc10_;
            _loc12_ = _loc9_;
            if(_loc11_ / _loc12_ > param1 / param2)
            {
               _loc4_ = param1;
               _loc5_ = param1 * _loc12_ / _loc11_;
            }
            else
            {
               _loc5_ = param2;
               _loc4_ = param2 * _loc11_ / _loc12_;
            }
            _loc8_ = true;
         }
         if(this._video.rotation == 0)
         {
            _loc6_ = (this._width - _loc4_) / 2;
            _loc7_ = (this._height - _loc5_) / 2;
         }
         else if(this._video.rotation == 90)
         {
            _loc6_ = (this._width - _loc4_) / 2 + _loc4_;
            _loc7_ = (this._height - _loc5_) / 2;
         }
         else if(this._video.rotation == 180)
         {
            _loc6_ = (this._width - _loc4_) / 2 + _loc4_;
            _loc7_ = (this._height - _loc5_) / 2 + _loc5_;
         }
         else if(this._video.rotation == -90)
         {
            _loc6_ = (this._width - _loc4_) / 2;
            _loc7_ = (this._height - _loc5_) / 2 + _loc5_;
         }
         
         
         
         if(this._mode == PlayerConstant.SCREEN_MODE_FULL)
         {
            _loc4_ = this._width;
            _loc5_ = this._height;
            if(this._video.rotation == 0)
            {
               _loc6_ = 0;
               _loc7_ = 0;
            }
            else if(this._video.rotation == 90)
            {
               _loc6_ = this._width;
               _loc7_ = 0;
            }
            else if(this._video.rotation == 180)
            {
               _loc6_ = this._width;
               _loc7_ = this._height;
            }
            else if(this._video.rotation == -90)
            {
               _loc6_ = 0;
               _loc7_ = this._height;
            }
            
            
            
         }
         this.changeView(_loc6_,_loc7_,_loc4_,_loc5_,param3,_loc8_);
      }
      
      protected function changeView(param1:Number, param2:Number, param3:Number, param4:Number, param5:Boolean, param6:Boolean) : void {
         var _loc7_:Number = this._core.currentVideoRealWidth;
         var _loc8_:Number = this._core.currentVideoRealHeight;
         if(!this._isRenderSize)
         {
            this._video.x = 0;
            this._video.y = 0;
            this._video.width = _loc7_;
            this._video.height = _loc8_;
            this._bakVideo.x = 0;
            this._bakVideo.y = 0;
            this._bakVideo.width = _loc7_;
            this._bakVideo.height = _loc8_;
            return;
         }
         var _loc9_:Number = param1;
         var _loc10_:Number = param2;
         if(PlayerConfig.specialType == PlayerConstant.SPECIAL_TYPE_SHENHE)
         {
            _loc9_ = 0;
            _loc10_ = 0;
         }
         var _loc11_:Number = param3;
         var _loc12_:Number = param4;
         if(param6)
         {
            _loc11_ = param4;
            _loc12_ = param3;
         }
         var _loc13_:Number = _loc7_ / 320 * _loc11_ / _loc7_;
         var _loc14_:Number = _loc8_ / 240 * _loc12_ / _loc8_;
         this.changeVideoView(this._video,param5,_loc9_,_loc10_,_loc13_,_loc14_);
         this.changeVideoView(this._bakVideo,param5,_loc9_,_loc10_,_loc13_,_loc14_);
      }
      
      private function changeVideoView(param1:Video, param2:Boolean, param3:Number, param4:Number, param5:Number, param6:Number) : void {
         Tweener.killTweensOf(param1);
         if((param2) && (param1.visible))
         {
            Tweener.to(param1,0.5,
               {
                  "x":param3,
                  "y":param4,
                  "scaleX":param5,
                  "scaleY":param6
               });
         }
         else
         {
            param1.x = param3;
            param1.y = param4;
            param1.scaleX = param5;
            param1.scaleY = param6;
         }
      }
      
      public function setAngle(param1:Number) : void {
         this._video.rotation = this._video.rotation + param1;
         this._bakVideo.rotation = this._bakVideo.rotation + param1;
         this._rotation = this._video.rotation;
         this.setScreenMode(this._mode,false);
      }
      
      public function get video() : Video {
         return this._video;
      }
   }
}
